﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Element.Net.Buttons
{
    public class ElementTabControl : TabControl
    {
        public ElementTabControl()
        {
            InitializeComponent();
        }

        private void InitializeComponent()
        {
            base.SetStyle(
            ControlStyles.UserPaint |                      // 控件将自行绘制，而不是通过操作系统来绘制
            ControlStyles.OptimizedDoubleBuffer |          // 该控件首先在缓冲区中绘制，而不是直接绘制到屏幕上，这样可以减少闪烁
            ControlStyles.AllPaintingInWmPaint |           // 控件将忽略 WM_ERASEBKGND 窗口消息以减少闪烁
            ControlStyles.ResizeRedraw |                   // 在调整控件大小时重绘控件
            ControlStyles.SupportsTransparentBackColor,    // 控件接受 alpha 组件小于 255 的 BackColor 以模拟透明
            true);                                         // 设置以上值为 true
            base.UpdateStyles();
            SizeMode = TabSizeMode.Fixed;// 固定长度大小
            base.BackColor = Color.White;
        }

        /// <summary>
        /// 文本布局格式
        /// </summary>
        private readonly StringFormat stringFormat = new StringFormat()
        {
            LineAlignment = StringAlignment.Center,
            Alignment = StringAlignment.Center
        };

        /// <summary>
        /// 选中颜色和刷子
        /// </summary>
        private Color selectedColor = Color.LimeGreen;
        private readonly SolidBrush selectedBrush = new SolidBrush(Color.LimeGreen);

        [Browsable(true)]
        public Color SelectedColor
        {
            set
            {
                this.selectedColor = value;
                this.selectedBrush.Color = value;
            }
            get { return this.selectedColor; }
        }

        /// <summary>
        /// 未选中颜色
        /// </summary>
        private Color unselectColor = Color.LightGray;
        private readonly SolidBrush unselectBrush = new SolidBrush(Color.LightGray);

        [Browsable(true)]
        public Color UnselectColor
        {
            set { this.unselectColor = value; }
            get { return this.unselectColor; }
        }

        /// <summary>
        /// 重绘tabcontrol
        /// </summary>
        /// <param name="e"></param>
        protected override void OnPaint(PaintEventArgs e)
        {
            e.Graphics.FillRectangle(new SolidBrush(Color.White), this.GetTabRect(0).X, this.GetTabRect(0).Y, this.Width, this.Height);// 背景白色
            for (int i = 0; i < this.TabCount; i++)
            {
                Rectangle rectangle = this.GetTabRect(i);
                if (this.SelectedIndex == i)
                {
                    e.Graphics.FillRectangle(selectedBrush, rectangle.X, rectangle.Y + this.ItemSize.Height, rectangle.Width, 2);// 选中底部绿色线
                    rectangle.Y += 2;
                    e.Graphics.DrawString(this.TabPages[i].Text, this.Font, selectedBrush, rectangle, stringFormat);// 选中字体绿色
                }
                else
                {
                    //e.Graphics.FillRectangle(unselectBrush, rectangle.X, rectangle.Y + this.ItemSize.Height - 2, rectangle.Width, 2);// 未选中底部灰色线
                    rectangle.Y += 2;
                    e.Graphics.DrawString(this.TabPages[i].Text, this.Font, unselectBrush, rectangle, stringFormat);// 未选中字体黑色
                }
            }
        }
    }
}
